package com.wx.mall.sys.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.sys.entity.LogisticsCompany;
import com.wx.mall.sys.service.ILogisticsCompanyService;
import com.wx.mall.sys.vo.reqVo.LogisticsCompanyPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 物流公司 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-03-25
 */
@RestController
@RequestMapping("/logisticsCompany")
@Api(tags = "物流公司相关接口")
public class LogisticsCompanyController {

    @Autowired
    private ILogisticsCompanyService logisticsCompanyService;

    @GetMapping("/logisticsCompanys")
    @ApiOperation("/分页查询")
    public CommonResult findPageInfo(@Valid LogisticsCompanyPageVO vo) {
        List<LogisticsCompany> page = logisticsCompanyService.findPage(vo);
        return CommonResult.success(CommonPage.restPage(page));
    };

@GetMapping("/getById")
@ApiOperation("根据id进行查询")
public CommonResult getById(Integer id){

LogisticsCompany list=logisticsCompanyService.findById(id);
return CommonResult.success(list);
}

    @DeleteMapping("/logisticsCompany/{id}")
        @ApiOperation("删除")
    public CommonResult deleyeById(@NotNull(message = "id不能为空") @PathVariable("id") Integer id) {
        boolean b = logisticsCompanyService.removeById(id);
        if (!b) {
            ApiAssert.fail("删除失败");
        }
        return CommonResult.success();
    }

    @PutMapping("/logisticsCompany")
    @ApiOperation("编辑接口")
    public CommonResult updateById(@Valid LogisticsCompany logisticsCompany) {
        boolean b = logisticsCompanyService.updateById(logisticsCompany);
        if (!b) {
            ApiAssert.fail("修改失败");
        }
        return CommonResult.success();
    }

    @PutMapping("/logisticsCompany/{id}/{state}")
    @ApiOperation("修改状态码")
    public CommonResult updateByIdState(@NotNull(message = "id不能为空") @PathVariable("id") Integer id,
                                        @NotNull(message = "状态码不能空") @PathVariable("state") Integer state) {

        logisticsCompanyService.updateByIdGetState(id, state);
        return CommonResult.success();
    }

    @PostMapping("/addList")
    @ApiOperation("新增")
    public CommonResult add(@Valid LogisticsCompany logisticsCompany) {
        logisticsCompany.setState(1);
        logisticsCompany.setCreateTime(new Date());
        boolean save = logisticsCompanyService.save(logisticsCompany);
        if (!save) {
            ApiAssert.fail("新增失败");
        }
        return CommonResult.success();
    };

@PostMapping("/updateList")
@ApiOperation("修改数据")
    public CommonResult updateList(LogisticsCompany logisticsCompany){
    System.out.println(logisticsCompany);
    int sucess=logisticsCompanyService.updateOne(logisticsCompany);

    return  sucess==1?CommonResult.success():CommonResult.failed();
}


}

